package com.module.my.model

import android.content.Context
import cn.leancloud.LCObject
import cn.leancloud.LCQuery
import com.module.app.base.BaseViewModel
import com.module.app.bus.UpdateBus
import com.module.app.ext.updateFlow
import com.module.app.network.getCommonService
import com.module.app.pop.ProgressPop2
import com.module.base.ext.download
import com.module.base.ext.request
import com.module.base.utils.CommonUtils
import com.module.base.utils.FileCommonUtils
import com.module.base.utils.ToastUtils
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.module.frame.ext.postEvent
import com.module.my.R
import com.module.third.learcloud.lcQuerySingle
import java.io.File

class AutoSkinNightViewModel : BaseViewModel() {

    var context: Context? = null

    fun getSkinNight() {
        request({
            val tClass = com.module.app.bean.SkinBean::class.java
            val query = LCQuery<LCObject>(tClass.simpleName)
            query.whereEqualTo("skinName", com.module.app.utils.AutoSkinNightUtils.skin_night)
            lcQuerySingle(query, tClass)
        }, {
            check(it != null) {
                CommonUtils.getString(R.string.error_unknow)
            }
            //直接执行下载
            downloadSkin(it)
        }, {
            ToastUtils.showShort(it.msg)
        }, true)
    }

    private fun downloadSkin(bean: com.module.app.bean.SkinBean) {
        if (context == null) {
            return
        }
        XXPermissions.with(context!!)
            .permission(Permission.READ_EXTERNAL_STORAGE)
            .permission(Permission.WRITE_EXTERNAL_STORAGE)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: List<String>, all: Boolean) {
                    if (all) {
                        ProgressPop2.show(context!!, "皮肤下载")
                        val path =
                            com.module.app.utils.FileUtils.getSkinPath() + File.separator + bean.skinName + ".skin"
                        FileCommonUtils.deleteFile(path)
                        download(path, {
                            postEvent(updateFlow, UpdateBus(0f, com.module.app.bean.IType.STATUS.start))
                            //执行下载
                            getCommonService().download(bean.download)
                        }, {
                            //进度
                            postEvent(updateFlow, UpdateBus(it, com.module.app.bean.IType.STATUS.progress))
                        }, {
                            //完成
                            postEvent(updateFlow, UpdateBus(100f, com.module.app.bean.IType.STATUS.end))
                            if (!it.isNullOrEmpty() && it.endsWith(".skin")) {
                                com.module.app.utils.MySkinUtils.addSDSkin(bean)
                                ToastUtils.showShortSafe("下载完成")
                            } else if (it.isNullOrEmpty()) {
                                ToastUtils.showShortSafe("下载异常")
                            }
                        }, {
                            //失败
                            postEvent(updateFlow, UpdateBus(100f, com.module.app.bean.IType.STATUS.end))
                            ToastUtils.showShortSafe("下载异常")
                        })
                    }
                }

                override fun onDenied(permissions: List<String>, never: Boolean) {
                    ToastUtils.showLong(R.string.permissions_write_read_never_reject_update)
                }
            })
    }
}